

(function(){
		
    var x = 0;
  
    function clickEvent(event) {
	  
		$gmap.getPossibleLocationWithLtdLng($gmap.geocoderRequestType.latLng, event.latLng, addPossibleLocationToInfoWindowHandler);
    }
    
    //nu metot ,autocoplete place textfield den bir yer seçildiginde yapılması gereken işleri yapar
    //
    function autoComplatePlaceHandler(_place){
    	var places = new Array();
    	places[0] = _place;
    	var oneLocationContent = createLocationContentForInfoWindow(0,_place);
    	var container  = addLocationToLocationContainer(oneLocationContent);
        var html = $("<div>").append(container).html();
		var infoWindowContent = html;
		$gmap.openInfoWindowOnMap(infoWindowContent,_place.geometry.location,infoWindowContentEventHandler,places);	
    
    }
    
    
    function addPossibleLocationToInfoWindowHandler(results,location) {	
		var possiblePlaceType = [];
		  possiblePlaceType.push($gmap.placeType.POLITICAL);
		  possiblePlaceType.push($gmap.placeType.LOCALITY);
		  //possiblePlaceType.push($gmap.placeType.SUBLOCALITY);
		
	    var locations = new Array();
	    var selectedPlaces = new Array();
	    var selectedPlaceNumber = 0;
	    var i = 0;
		for(i = 0; i <results.length;i++){
			var isContain = isContainPlaceType(results[i].types,possiblePlaceType);
			if(isContain){
			var oneLocationContent = createLocationContentForInfoWindow(selectedPlaceNumber,results[i]);
			locations[selectedPlaceNumber] = oneLocationContent;
			selectedPlaces[selectedPlaceNumber] = results[i];
			selectedPlaceNumber++;
			}
		}
		var locationContainer = addLocationToLocationContainer(locations);
	var html = $("<div>").append(locationContainer).html();
		var infoWindowContent = html;
		$gmap.openInfoWindowOnMap(infoWindowContent,location,infoWindowContentEventHandler,selectedPlaces);
	    //$gmap.$gmap.addEventToMap($gmap.gmapEventType.CLICK, clickEvent);	
	}
	
	function infoWindowContentEventHandler(results){
          
		  $("#locationContainer").find(".selectCheckBox").each(function(index) {
			 var thisSelectBox = $(this);
			 $(this).click(function() {
				 var name = thisSelectBox.attr("name");
				 var id = thisSelectBox.attr("id");
			     var  selectedResultNo = name.split(":")[1];
			      if(thisSelectBox.attr("checked")){
			      	addSelectedCityMarker(results[selectedResultNo],thisSelectBox);
			      
			      }else{
			      	deletedSelectedCityMarker(results[selectedResultNo],thisSelectBox);
			      } 
				 });
		  });
		 x++;
   }
   
   function addCityMarkerHandler(response){
   	  if(response.success == true){
   	  	  alert(response.message);
   	  }
   }
    function deleteCityMarkerHandler(response){
    	i
    }
	function addLocationToLocationContainer(onelocations){
    	var locationContatiner = $("<div>").attr({'class':'locationContainer','id':'locationContainer'});
	    var locationContainerHeader = $("<div>").attr({'class':'locationContainerHeader'});
	    var locationContainerHeaderName = $("<div>").attr({'class':'locationContainerHeaderName'});
	    var locationContainerHeaderLabel = $("<div>").attr({'class':'labelContainer'});
	    var labeltext1 = $("<span>").html("Been").addClass('locationHeaderLabelSpan');
	    var labeltext2 = $("<span>").html("Go").addClass('locationHeaderLabelSpan');
	    var labeltext3 = $("<span>").html("Favorite").addClass('locationHeaderLabelSpan');
	    locationContainerHeaderLabel.append(labeltext1).append(labeltext2).append(labeltext3);
	    locationContainerHeader.append(locationContainerHeaderName ).append(locationContainerHeaderLabel);
	    locationContatiner.append(locationContainerHeader);
	    for(i = 0; i < onelocations.length; i++){
	    	locationContatiner.append(onelocations[i]);
	    }
	    return locationContatiner;
    }
	
    function createLocationContentForInfoWindow(i,content){
      var divLocationContainer = $("<div>").attr({'id':'location'+i,'class':"oneLocation"});
      var divCityName = $("<div>").attr({'id':"cityName"+i,'class':"locationName"});
               $(divCityName).append("<span>"+content.formatted_address+"</span>");
      var checkBoxContainer = $("<div>").attr({'id':'checkBoxContainer'+i,'class':'checkBoxContainer'})
      var beenSelectCheckBox = $("<input>").attr({'type':"checkBox",
                                                  'name':"beenSelect:"+i,
                                                  'value':"beenSelect",
                                                  'class':"selectCheckBox"
                                                 }).addClass('selectCheckBox');
      var goSelectCheckBox = $("<input>").attr({'type':"checkBox",
                                                'name':"goSelect:"+i,
                                                'value':"goSelect",
                                                'class':"selectCheckBox"});
                                                
      var favoriteSelectCheckBox = $("<input>").attr({'type':"checkBox",
                                                    'name':"favoriteSelect:"+i,
                                                    'value':"favoriteSelect",
                                                    'class':"selectCheckBox"});
      checkBoxContainer.append(beenSelectCheckBox).append(goSelectCheckBox).append(favoriteSelectCheckBox);
      divLocationContainer.append(divCityName).append(checkBoxContainer);
      return  divLocationContainer;                                           
	}
  function isContainPlaceType(selectedPlaceType , avaiblePlaceType){
  	     var isContain = false;
  	     for(i = 0; i < avaiblePlaceType.length; i++){
  	     	var placeType = avaiblePlaceType[i];
  	     	for(j = 0; j < selectedPlaceType.length; j++){
  	     	if(placeType == selectedPlaceType[j]){
  	     		isContain = true;
  	     		return isContain;  	     	}
  	     	
  	     	}
  	     }
  	     return false;
  }
  function addSelectedCityMarker(selectedCity,selectedCheckBox){
   	  var seletedCityType = -1;
   	  var selectedCheckBoxVal = selectedCheckBox.attr("value");
   	  if(selectedCheckBoxVal  == "goSelect"){
   	  	 selectedCityType = $appConst.cityType.cityGoType;
   	  	 
   	  }else if(selectedCheckBoxVal  == "favoriteSelect"){
   	  	  selectedCityType = $appConst.cityType.cityFavoriteType;
   	  }else if(selectedCheckBoxVal  == "beenSelect"){
   	  	   selectedCityType = $appConst.cityType.cityBeenType;
   	  }
   	  
   	  
   	  
   	  
   	 var selectedCityJson = {
   	 	'latitude':selectedCity.geometry.location.lat(),
   	 	'longitute':selectedCity.geometry.location.lng(),
   	 	'cityName':selectedCity.formatted_address
   	 }
   	 
   	 var request = {
   	   'userId':$appRequest.requestUserId,
   	   'cityType':selectedCityType,
   	   'selectedCity':selectedCityJson
   	 }
   	 	
   	 var requestString = JSON.stringify(request); 
   	 $appRequest.addCityUser(requestString,addCityMarkerHandler);
   	 createMarkerForCity(selectedCity);
   }
     
   function deletedSelectedCityMarker(){
   	   
   }
   function createMarkerForCity(selectedCity,cityType){
   	   var image = "";
   	   var location = selectedCity.geometry.location;
       var imageUrl = "/public/images/defaultMarker.png";
       var title = selectedCity.formatted_address.split(",")[0];
       $gmap.addMarkerToMap(location,imageUrl,title);
   	
   }
   function initlialize(){
   	    $gmap.setLatitude(38.36869).setLongitude().mapInit();
		$gmap.addEventToMap($gmap.gmapEventType.CLICK, clickEvent);
		var placeType =  $gmap.placeType;
		var options= {};
		var input = document.getElementById("searchTextField");
		var placeTypes = [$gmap.placeType.POLITICAL];
		$gmap.addAutoCompletePlaceInput(input,autoComplatePlaceHandler,placeTypes);
   }
   
 $(document).ready(function(){
  initlialize(); 
  });      
	    
})();